package com.quectel.core.module.alarm.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.core.module.alarm.dao.AlarmRuleTenantConditionDao;
import com.quectel.core.module.alarm.dto.AlarmRuleTenantConditionDto;
import com.quectel.core.module.alarm.entity.AlarmRuleTenantConditionEntity;
import com.quectel.core.module.alarm.service.AlarmRuleTenantConditionService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-10-19 14:09:09
 */
@DubboService
public class AlarmRuleTenantConditionServiceImpl implements AlarmRuleTenantConditionService {

    private static final Logger LOGGER = LoggerFactory.getLogger(AlarmRuleTenantConditionServiceImpl.class);

    @Autowired
    private AlarmRuleTenantConditionDao alarmRuleTenantConditionDao;


    @Override
    public void saveOrUpdate(AlarmRuleTenantConditionDto alarmRuleTenantConditionDto) {
        AlarmRuleTenantConditionDto dbValue = selectByTenantIdAndRuleId(alarmRuleTenantConditionDto.getTenantId(), alarmRuleTenantConditionDto.getRuleId());
        if (dbValue != null) {
            alarmRuleTenantConditionDto.setId(dbValue.getId());
            updateById(alarmRuleTenantConditionDto);
        } else {
            save(alarmRuleTenantConditionDto);
        }
    }

    @Override
    public AlarmRuleTenantConditionDto selectByTenantIdAndRuleId(Long tenantId, Long ruleId) {

        AlarmRuleTenantConditionEntity entity = alarmRuleTenantConditionDao.selectOne(
                new LambdaQueryWrapper<AlarmRuleTenantConditionEntity>()
                        .eq(AlarmRuleTenantConditionEntity::getTenantId, tenantId)
                        .eq(AlarmRuleTenantConditionEntity::getRuleId, ruleId)
                        .last(SqlConstants.MYSQL_LIMIT_ONE)
        );
        return CopyUtils.copyObj(entity, AlarmRuleTenantConditionDto.class);
    }


    @Override
    public List<AlarmRuleTenantConditionDto> queryList(Map<String, Object> params) {
        List<AlarmRuleTenantConditionEntity> list = alarmRuleTenantConditionDao.queryList(params);
        return CopyUtils.copyList(list, AlarmRuleTenantConditionDto.class);
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return alarmRuleTenantConditionDao.queryTotal(params);
    }

    @Override
    public Long save(AlarmRuleTenantConditionDto dto) {
        AlarmRuleTenantConditionEntity entity = CopyUtils.copyObj(dto, AlarmRuleTenantConditionEntity.class);
        entity.setId(Snowflake.nextId());
        alarmRuleTenantConditionDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(AlarmRuleTenantConditionDto dto) {
        AlarmRuleTenantConditionEntity entity = CopyUtils.copyObj(dto, AlarmRuleTenantConditionEntity.class);
        alarmRuleTenantConditionDao.updateById(entity);
    }


    @Override
    public AlarmRuleTenantConditionDto selectById(Long id) {
        AlarmRuleTenantConditionEntity entity = alarmRuleTenantConditionDao.selectById(id);
        return CopyUtils.copyObj(entity, AlarmRuleTenantConditionDto.class);
    }

    @Override
    public void deleteById(Long id) {
        alarmRuleTenantConditionDao.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
